package com.happypets.backstage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.servicesImpl.UserServiceImpl;

/**
 * Servlet implementation class DealInfoPage
 */
public class DealInfoPage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DealInfoPage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		UserServiceImpl psi = new UserServiceImpl();
		
		if(request.getParameter("flag") != null){
			
			String formStatus = request.getParameter("form-status");
			String formId = request.getParameter("form-id");
			String formDealer = request.getParameter("form-dealer");
			
			psi.updateFormStatus(formId, formStatus, formDealer);
			
			response.sendRedirect("backstage/pages/deal.html");
			
		}else{
		
			PrintWriter out = response.getWriter();
			
			List<Map<String,String>> lm = null;
			String pageShowNumber = request.getParameter("pageShowNumber");
			String currentPage = request.getParameter("currentPage");
			
			lm = psi.dealInfoPage(Integer.parseInt(pageShowNumber), Integer.parseInt(currentPage));
			
			
			
			Gson gson=new Gson();
			String json=gson.toJson(lm);
			System.out.println("deal ----\n"+json);
			out.write(json);
			
			out.flush();
			out.close();
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
